package org.wmq.transferordersystem.dao.impl;

import com.mongodb.client.result.UpdateResult;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import org.wmq.transferordersystem.dao.LogisticsDao;
import org.wmq.transferordersystem.model.Entity.Logistics;
import org.wmq.transferordersystem.model.Entity.Order;

@Repository
@RequiredArgsConstructor
public class LogisticsDaoImpl implements LogisticsDao {
    private final MongoTemplate mongoTemplate;

    @Override
    public boolean insert(Logistics logistics) {
        Criteria queryCriteria = Criteria.where("id").is(logistics.getOrderId());
        Update update = new Update();
        update.set("status", logistics.getOperation());
        update.push("logistics", logistics);
        UpdateResult updateResult = this.mongoTemplate.updateFirst(new Query(queryCriteria), update, Order.class);
        return updateResult.getModifiedCount() == 1;
    }
}
